package com.dongdongshop.seller.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("login")
public class LoginController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);
    //跳转到登录页面
    @RequestMapping("toLogin")
    public String toLogin(){
        return "shoplogin";
    }

    //登录
    @RequestMapping("dengLu")
    public String dengLu(String sellerId, String password, Model model){
        //1,获取subject
        Subject subject= SecurityUtils.getSubject();
        //2,将参数封装成token对象
        UsernamePasswordToken token = new UsernamePasswordToken(sellerId, password);
        //3,执行登录方法
        try {
            subject.login(token);
            return "redirect:/login/toIndex";//登录成功
        }catch (UnknownAccountException e){//账号(用户不存在)
            logger.info("异常>>>>>",e);
            model.addAttribute("message","用户不存在或正在审核");
            return "shoplogin";
        }catch (IncorrectCredentialsException e){//密码不正确
            logger.info("异常密码>>>>>",e);
            model.addAttribute("message","密码不正确");
            return "shoplogin";
        }
    }
    //登录后跳转到首页
    @RequestMapping("toIndex")
    public String toIndex(){
        return "admin/index";
    }
}
